package com.doyoo.config;

import io.kubernetes.client.Metrics;
import io.kubernetes.client.PodLogs;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.*;
import io.kubernetes.client.openapi.models.V1beta1CronJob;
import io.kubernetes.client.openapi.models.V1beta1CronJobList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import io.kubernetes.client.util.generic.options.DeleteOptions;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.net.URL;

@Configuration
@EnableConfigurationProperties(JobProperties.class)
public class K8sConfig {

    @Resource
    ResourceLoader resourceLoader;

    @PostConstruct
    public void apiClient() throws IOException {
        String clientType = System.getenv("k8s.clientType");
        if (StringUtils.equals(clientType, "kube-config-file")) {
            FileReader fileReader = new FileReader(resourceLoader.getResource("classpath:kubeconfig").getFile());
            ApiClient client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(fileReader)).build();
            io.kubernetes.client.openapi.Configuration.setDefaultApiClient(client);
        } else {
            ApiClient client = ClientBuilder.cluster().build();
            io.kubernetes.client.openapi.Configuration.setDefaultApiClient(client);
        }
    }

    @Bean
    public CustomObjectsApi customObjectsApi() {
        return new CustomObjectsApi();
    }

    @Bean
    public NodeApi nodeApi() {
        return new NodeApi();
    }

    @Bean
    public ExtensionsApi extensionsApi() {
        return new ExtensionsApi();
    }

    @Bean
    public BatchV1Api batchV1Api() {
        return new BatchV1Api();
    }

    @Bean
    public BatchV1beta1Api batchV1beta1Api() {
        return new BatchV1beta1Api();
    }

    @Bean
    public CoreV1Api coreV1Api() {
        return new CoreV1Api();
    }

    @Bean
    public Metrics metrics() {
        return new Metrics();
    }

    @Bean
    public AppsV1Api appsV1Api() {
        return new AppsV1Api();
    }

    /////////////////////////////////

    @Bean
    public PodLogs podLogs() {
        return new PodLogs();
    }

    @Bean
    public GenericKubernetesApi<V1beta1CronJob, V1beta1CronJobList> cronJobGenericApi() {
        GenericKubernetesApi<V1beta1CronJob, V1beta1CronJobList> cronJobGenericApi =
                new GenericKubernetesApi<>(V1beta1CronJob.class, V1beta1CronJobList.class,
                        "batch", "v1beta1", "cronjobs",
                        io.kubernetes.client.openapi.Configuration.getDefaultApiClient());
        return cronJobGenericApi;
    }

    @Bean
    public DeleteOptions deleteOptions() {
        DeleteOptions deleteOptions = new DeleteOptions();
        deleteOptions.setPropagationPolicy("Foreground");
        return deleteOptions;
    }
}
